package com.itheima.health.service.impl;

import com.itheima.health.dao.OrdersettingDao;
import com.itheima.health.pojo.vo.OrderSettingVo;
import com.itheima.health.pojo.entity.OrderSetting;
import com.itheima.health.service.OrdersettingService;
import lombok.extern.slf4j.Slf4j;
import org.apache.tomcat.jni.Local;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.ZoneOffset;
import java.util.List;

@Slf4j
@Service
public class OrdersettingServiceImpl implements OrdersettingService {
    @Autowired
    private OrdersettingDao ordersettingDao;

    @Override
    public List<OrderSettingVo> getOrderSettingByMonth(Integer year, Integer month){
        LocalDate begin = LocalDate.of(year, month, LocalDate.now().getDayOfMonth());
        LocalDate end= LocalDate.of(year, month,31);
        List<OrderSettingVo> voList = ordersettingDao.getOrderSetting(begin,end);
        for (OrderSettingVo vo : voList) {
           Integer day = vo.getOrderDate().getDayOfMonth();
           vo.setDate(day);
        }
        return voList;
    }
    @Override
    public void addOrderSetting(OrderSetting orderSetting) {
        LocalDate l=orderSetting.getOrderDate().toInstant().atZone(ZoneOffset.UTC).toLocalDate();
        LocalDate localDate = l.plusDays(1);
        List<OrderSetting> exist=ordersettingDao.existDate(localDate);
        if (exist!=null&&exist.size()>0){
            // TODO 修改设置时间的人数
            ordersettingDao.updateFuking(localDate, orderSetting.getNumber());
        }else {
            ordersettingDao.insertfuckingSearchFor(localDate, orderSetting.getNumber());
        }
//          if (getNumber < orderSetting.getNumber()) {
//              throw new BusinessRuntimeException(MessageConst.ORDERSETTING_FAIL);
//          }
//        ordersettingDao.updateFuking(orderSetting);
    }
}
